package com.mryx.demo.util;

import java.util.HashMap;
import java.util.Random;
import java.util.Set;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.springframework.stereotype.Component;

@Component("senderCode")
public class SDKTestSendTemplateSMS {

	public String SendVerCode(String phoneNum) {
		String fourRandom=getFourRandom();
		HashMap<String, Object> result = null;
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		restAPI.init("app.cloopen.com", "8883");
		restAPI.setAccount("8a216da86904c06001690856374d00c3", "ad46292394e5474db0f46c923380c211");
		restAPI.setAppId("8a216da86904c0600169085637a800ca");
		result = restAPI.sendTemplateSMS(phoneNum,"1" ,new String[]{fourRandom,"5"});

		System.out.println("SDKTestGetSubAccounts result=" + result);
		if("000000".equals(result.get("statusCode"))){
			//正常返回输出data包体信息（map）
			HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
			Set<String> keySet = data.keySet();
			for(String key:keySet){
				Object object = data.get(key);
				System.out.println(key +" = "+object);
			}
		}else{
			//异常返回输出错误码和错误信息
			System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
		}
		return fourRandom;
	}

	public String getFourRandom() {
		Random random = new Random();
		String fourRandom = random.nextInt(10000) + "";
		int randLength = fourRandom.length();
		if (randLength < 4) {
			for (int i = 1; i <= 4 - randLength; i++) {
				fourRandom = "0" + fourRandom;
			}
		}
		return fourRandom;
	}
}
